package offer

import (
	"bp/internal/render"
	"bp/internal/service/param"
	"bp/internal/util"
	offerGrpc "bp/rpc/gen/mwebapi/offer"
	"context"
)

// ListOffersOfOwner 列出所有者(品牌或门店)可用的优惠项目
func (s *OfferMWebApiImp) ListOffersOfOwner(ctx context.Context, req *offerGrpc.ListOffersOfOwnerReq) (res *offerGrpc.ListOffersOfOwnerRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("offer_imp/query.ListOffersOfOwner=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &offerGrpc.ListOffersOfOwnerRes{}
	q := s.dao.Q

	cPtr := param.GenListCommonParamFromContext(ctx)
	ptr, err := s.VldListOffersOfOwnerReq(ctx, q, req)
	if err != nil {
		return
	}
	offerPtrs, err := s.comHub.OfferService.ListOfferPtrsOfOwner(ctx, q, cPtr, ptr)
	if err != nil {
		return
	}

	res.Offers = s.ConvOfferPtrArrayEntityToPb()(offerPtrs)
	return
}

// ListAvailableOffersOfItemID 列出商品可用的优惠项目
func (s *OfferMWebApiImp) ListOffersOfItemID(ctx context.Context, req *offerGrpc.ListOffersOfItemIDReq) (res *offerGrpc.ListOffersOfItemIDRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("offer_imp/query.ListOffersOfItemID=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &offerGrpc.ListOffersOfItemIDRes{}
	q := s.dao.Q

	cPtr := param.GenListCommonParamFromContext(ctx)
	ptr, err := s.VldListOffersOfItemIDReq(ctx, q, req)
	if err != nil {
		return
	}
	offerPtrs, err := s.comHub.OfferService.ListOfferPtrsOfItemID(ctx, q, cPtr, ptr)
	if err != nil {
		return
	}

	res.Offers = s.ConvOfferPtrArrayEntityToPb()(offerPtrs)
	return
}
